﻿using System;
using AutoMapper;
using Common.Views;
using Sanelib.Common.Helpers;
using Sanelib.DataView;

namespace WebApp.Initialization.Automapper.Resolvers
{
    public class MemberCodeToIdResolver : ValueResolver<string, Guid?>
    {
        private readonly IViewRepository<MemberView> _memViewRepository;

        public MemberCodeToIdResolver(IViewRepository<MemberView> memberViewRepository)
        {
            _memViewRepository = memberViewRepository;
        }

        protected override Guid? ResolveCore(string source)
        {
            var memberView = _memViewRepository.GetByKey(Property.Of<MemberView>(x => x.Code), source);
            return memberView == null ? (Guid?) null : memberView.Id;
        }
    }
}